/*
 * @Author: zqx me_zqx@163.com
 * @Date: 2025-04-27 17:45:04
 * @LastEditors: zqx me_zqx@163.com
 * @LastEditTime: 2025-09-15 18:44:28
 * @FilePath: /XMate-h5/src/utils/electronTool.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

import { setToken, setUserInfo } from "./auth";

let windowName = "";
export function loadInfoFromPC(cb) {
  try {
    const ipcRenderer = window.nodeRequire("electron").ipcRenderer;
    const rid = "xmate-h5";
    console.log("loadInfoFromPC");
    ipcRenderer.send("pageReady", rid);
    ipcRenderer.once("page-opened", (_, { id, data }) => {

      if (id === rid) {
        data = JSON.parse(JSON.stringify(data));
        console.log("data", data);
        const userInfo =JSON.parse( data['yxt-userInfo']);
        windowName = data['windowName'];
        
        const { token } = userInfo;
        setUserInfo(userInfo);
        setToken(token);
        if (cb) {
          cb();
        }
      }
    });
  } catch (e) {
    console.log("非electron环境", e);
  }
}

// 关闭当前窗口
export function closeWindow() {
  console.log('closeWindow: ', windowName);
  const ipcRenderer = window.nodeRequire("electron").ipcRenderer;
  ipcRenderer.send("forward_message", {
    to: "hide_window",
    message: {"action":'close_current_window', data: windowName}
  });
}
